//顶部轮播图
mui.getJSON('http://www.99990158.com/ajax/GetArticlePageList.aspx', {
	page: 1,
	size: 10,
	cid: 118,
	channel: 'CeHuaZhuanTi',
	is_top: 1
}, function(rps) {
	//清除本地html
	document.getElementById('swiper').innerHTML = '';
	//用swiper
	var lis = [];
	mui.each(rps.data, function(index, item) {
		var div = document.createElement('div');
		div.className = 'swiper-slide';

		var a = document.createElement('a');
		a.addEventListener('tap', function(e) {
			openShareView('http://99990158.com/cehua.aspx?id=' + item.id, item.title,item.zhaiyao);
		});

		var img = document.createElement('img');
		img.src = setPre(item.img_url);

		a.appendChild(img);
		div.appendChild(a);

		document.getElementById('swiper').appendChild(div);

	})
	var swiper = new Swiper('#swiper-container', {
		pagination: '#swiper-pagination',
		paginationClickable: true,
		loop: true,
		autoplay: 8000
	});

	/*$('#swiper img').width(swiper.width);
	$('#swiper img').height(182);*/

	return;
	var modelHtml = document.getElementById('topModel').innerHTML;
	var lis = [];
	var lis2 = [];
	mui.each(rps, function(index, item) {
		var html = modelHtml.replace('{{img}}', 'http://www.99990158.com' + item.livebanner);
		html = html.replace(/{{title}}/g, item.title);
		html = html.replace(/{{url}}/g, 'http://www.99990158.com/cehua.aspx?id=' + item.id)

		lis.push(html);
		//mui('.mui-content').append('<li><a href="http://99990158.com/cehua.aspx?id=' + item.id + '" target="_blank"><img src="http://99990158.com' + item.img_url + '"></a></li>');

		lis2.push('<div class="mui-indicator"></div>');
	});
	var f = lis[0];
	var l = lis[lis.length - 1];
	lis.unshift(l);
	lis.push(f);

	document.getElementById('topImg').innerHTML = lis.join(' ');
	var len = document.getElementById('topImg').children.length;
	document.getElementById('topImg').children[0].className += ' mui-slider-item-duplicate';
	document.getElementById('topImg').children[len - 1].className += ' mui-slider-item-duplicate';

	document.getElementById('topImg2').innerHTML = lis2.join('');
	document.getElementById('topImg2').children[0].className += ' mui-active';

});

/*行情*/
var marketData = undefined;
GetMarket();
window.setInterval(GetMarket, 10000);

var swiper2 = null;

function GetMarket() {

	/*mui.getJSONP('http://gold.500.com/market/getmarketlistdata?v=1489373208758',{},function(rps){
			console.log(rps); 
			
		});*/

	var data = {
		v: Date.parse(new Date())
	};
	//http://liveapi.99990158.com/api/Gold500/getmarketlistdata
	$.get('http://liveapi.99990158.com/Ajax/getmarketlistdata.aspx', data, function(data) {
		var obj = JSON.parse(data);
		if(marketData == undefined) {
			marketData = data;
		} else if(marketData == data) {
			 
			return;

		}
		//清除本地html
		//document.getElementById('swiper_m').innerHTML = '';
		var lis = [];
		mui.each(obj.data.list, function(index, item) {
			var li = document.createElement('li');
			li.className = 'mui-table-view-cell mui-media mui-col-xs-4';

			var a = document.createElement('a');
			a.setAttribute('onclick', 'openMarket("' + item.productcode + '", "' + item.productname + '");')

			if(item.updown > 0) {
				a.innerHTML = '<h4 code=' + item.productcode + '>' + item.productname + '</h4><h2 class="redfont">' + item.newprice.toFixed(2) +
					'</h2><div class="mui-media-body redfont" >+' + item.updown.toFixed(2) + '<span>&nbsp;+' + (item.updownrate * 100).toFixed(2) + '%</span></div>';
			} else {
				a.innerHTML = '<h4 code=' + item.productcode + '>' + item.productname + '</h4><h2 class="grennfont">' + item.newprice.toFixed(2) +
					'</h2><div class="mui-media-body grennfont" >-' + Math.abs(item.updown.toFixed(2)) + '<span>&nbsp;-' + Math.abs((item.updownrate * 100).toFixed(2)) + '%</span></div>';
			}

			li.appendChild(a);

			$('#swiper_m').find('li').eq(index).html(li.innerHTML);
			lis.push(li.innerHTML);

			//document.getElementById('swiper_m').appendChild(div);
		});

		if(swiper2 == null) {
			swiper2 = new Swiper('#swiperM-container', {
				/*pagination: '#swiper_m-pagination',
				paginationClickable: true,*/
				loop: true,
				autoplay: 5000
			});

			/*mui.each(mui('#swiper_m a'), function(index, element) {
				element.addEventListener('tap', function(e) {
					var title = element.querySelector('h4').innerText;
					var code = element.querySelector('h4').getAttribute('code');
					openMarket(code, title);
				});
			})*/
		}

		/*mui.each(mui('#swiper_m .mui-table-view-cell'), function(index, element) {
			element.innerHTML = lis[index];
		})*/
		/*	$.each($('#swiper_m li'), function(index,item) {
				$(item).html(lis[index].innerHTML);
				 
			}); */

		/*var div = document.createElement('div');
		div.className = 'swiper-slide';
		var ul1 = document.createElement('ul');
		ul1.className = 'mui-table-view mui-grid-view';
		ul1.appendChild(lis[0]);
		ul1.appendChild(lis[1]);
		ul1.appendChild(lis[2]);
		div.appendChild(ul1);
		document.getElementById('swiper_m').appendChild(div);

		div = document.createElement('div');
		div.className = 'swiper-slide';
		ul1 = document.createElement('ul');
		ul1.className = 'mui-table-view mui-grid-view';
		ul1.appendChild(lis[3]);
		ul1.appendChild(lis[4]);
		ul1.appendChild(lis[5]);
		div.appendChild(ul1);
		document.getElementById('swiper_m').appendChild(div);*/

		return;

		var obj = JSON.parse(data);
		if(marketData == undefined) {
			marketData = data;
		} else if(marketData == data) {
			console.log('数据无更新');
			return;

		}

		var lis = [];

		//lis.push('<div class="mui-slider-item"><ul class="mui-table-view mui-grid-view">');
		mui.each(obj.data.list, function(index, item) {
			if(index % 3 == 0) {
				lis.push('<div class="mui-slider-item"><ul class="mui-table-view mui-grid-view">');
			}
			var html = '';
			html += '<li class="mui-table-view-cell mui-media mui-col-xs-4">';
			html += '<a  onclick="openMarket(\'' + item.productcode + '\',\'' + item.productname + '\')">';
			html += '<h4>' + item.productname + '</h4>';

			if(item.updown > 0) {
				html += '<h2 class="redfont">' + item.newprice.toFixed(2) + '</h2>';
				html += '<div class="mui-media-body redfont" id="datazf">+' + item.updown.toFixed(2) + '<span>+' + (item.updownrate * 100).toFixed(2) + '%</span></div>';
			} else {
				html += '<h2 class="grennfont">' + item.newprice.toFixed(2) + '</h2>';
				html += '<div class="mui-media-body grennfont" id="datazf">' + item.updown.toFixed(2) + '<span>' + (item.updownrate * 100).toFixed(2) + '%</span></div>';
			}

			html += '</a></li>';
			lis.push(html);

			if((index + 1) % 3 == 0) {
				lis.push('</ul></div>');
				//lis=[];
			}
		});

		/*lis.push(lis[0]);
		lis.unshift(lis[2]);*/
		var dddd = document.getElementById("slider_market");
		dddd.innerHTML = lis.join(" ");

		/*mui-slider-item-duplicate*/
		var firstChild = dddd.firstChild;
		var lastChild = dddd.lastChild;
		var newFirstChild = firstChild.cloneNode(true);
		var newLastChild = dddd.lastChild.cloneNode(true);
		newFirstChild.className = 'mui-slider-item mui-slider-item-duplicate';
		newLastChild.className = 'mui-slider-item mui-slider-item-duplicate';

		dddd.insertBefore(newLastChild, dddd.firstChild);
		dddd.appendChild(newFirstChild, lastChild);

		slider_market.slider({
			interval: 5000
		});

		document.getElementById("slider_market").addEventListener('tap', function() {
			//plus.webview.getWebviewById('examples/market.html').show();
		});
		//console.log(dddd.innerHTML);

	});
}

//重要行情
Important();

function Important() {
	$.getJSON("http://www.99990158.com/ajax/GetArticle.ashx", {
		id: 1654
	}, function(item) {

		var obj = JSON.parse(item.content.replace(/\\"/g, "\""));

		if(obj.link_url) {
			document.getElementById('Important').querySelector('a').addEventListener('tap', function() {
				openOtherView(obj.link_url, obj.title);
			});
		}
		if(obj.img != "") {
			document.getElementById('Important').querySelector('img').src = obj.img;
		}
		document.getElementById('Important').querySelector('.mui-ellipsis').innerHTML = (obj.title + "").substr(0, 20);

		setInterval(function() {
			GetRTime(new Date(obj.dateTime));

		}, 1000);

		return;

		var date = new XDate(2017, 05, 28);

		setInterval(function() {
			var nowDate = new XDate();
			var day = nowDate.diffDays(date); // 31
			var hour = nowDate.diffHours(date); // 31
			var minutes = nowDate.diffMinutes(date); // 31
			var seconds = nowDate.diffSeconds(date); // 31
			document.getElementById('divdown4').innerText = day;
			document.getElementById('divdown3').innerText = hour;
			document.getElementById('divdown2').innerText = minutes;
			document.getElementById('divdown1').innerText = seconds;

		}, 1000);
	});

}

function GetRTime(endTime) {
	var EndTime = endTime;
	var NowTime = new Date();
	var t = EndTime.getTime() - NowTime.getTime();
	var d = 0;
	var h = 0;
	var m = 0;
	var s = 0;
	if(t >= 0) {
		d = Math.floor(t / 1000 / 60 / 60 / 24);
		h = Math.floor(t / 1000 / 60 / 60 % 24);
		m = Math.floor(t / 1000 / 60 % 60);
		s = Math.floor(t / 1000 % 60);
	}

	document.getElementById("divdown4").innerHTML = d + "";
	document.getElementById("divdown3").innerHTML = h + "";
	document.getElementById("divdown2").innerHTML = m + "";
	document.getElementById("divdown1").innerHTML = s + "";
}

GetArticleTopList();

/*精评*/
function GetArticleTopList() {
	mui.getJSON('http://www.99990158.com/ajax/GetArticlePageList.aspx', {
		page: 1,
		size: 6,
		cid: 83,
		channel: 'news',
		sort: 'add_time desc'
	}, function(res) {

		var lis = [];
		mui.each(res.data, function(index, item) {

			var html = "";
			html += '<li ta="newsneiy.html?id=' + item.id + '" class="mui-table-view-cell" onclick="openWindowWithTitle(\'newsneiy.html?id=' + item.id + '\',\'中诚精评\')">';
			html += '<p class="fbtitle">' + item.title + '</p>';
			html += '<p class="fabuxx"><span class="fbauthor">' + item.source + ' ' + item.author + '</span><span class="fbtime">' + GetTime(item.add_time) + '</span></p>';
			html += '</li>';
			lis.push(html);
		});
		var aaa = document.getElementById("ArticleTopList");
		aaa.innerHTML = lis.join(" ");

		//SetPullRefresh(document.getElementById('item1mobile').querySelector('.mui-scroll'));

	});

}

//实时资讯
/*setInterval(GetNewLive, 5000);*/

function GetNewLive() {
	$.getJSON('http://www.99990158.com/ajax/GetNewsLive.ashx?page=1', function(res) {

		//假设你的列表返回在data集合中
		var list = _.sortBy(res, function(n) {
			return -n.createdAt;
		});

		//模板
		var t = document.getElementById('liveModel').innerHTML;

		//只要前5条
		var lis = [];

		$.each(list, function(index, item) {
			if(index <= 5) {
				lis.push(list[index]);
			} else {
				return false;
			}
		});

		var res = tmpl(t, lis, tmpl_count(lis), tmpCallBack);
		document.getElementById('item2mobile').querySelector('.mui-scroll').innerHTML = '<ul class="mui-table-view">' + res + '</ul>';

	});

}
/*setInterval(GetFin, 5000);*/

function GetFin() {
	$.getJSON('http://www.99990158.com/ajax/GetZhongDianRili.ashx', {
		v: Date.parse(new Date())
	}, function(res) {

		//假设你的列表返回在data集合中
		var list = _.sortBy(res, function(n) {
			return -n.createdAt;
		});

		//模板
		var t = document.getElementById('finModel').innerHTML;

		//只要前5条
		var lis = [];
		$.each(list, function(index, item) {
			if(index <= 5) {
				lis.push(list[index]);
			} else {
				return false;
			}
		});

		var res = tmpl(t, lis, tmpl_count(lis), finCallBack);
		document.getElementById('item3mobile').querySelector('.mui-scroll').innerHTML = '<ul class="mui-table-view">' + res + '</ul>';
		$.each($('#item3mobile').find('img'), function(index, img) {
			$(img).addClass('mui-media-object mui-pull-left');
			if($(img).attr('src') != '../images/fin.png') {

				$(img).attr('src', "http://www.caijingriliba.com/" + $(img).attr('src'));
			}

		});

	});
}

function SetPullRefresh(id) {

}

function blockOpen() {
	// 阻塞网络图片模式打开页面
	var w = plus.webview.create("news.html", "csdn", {
		blockNetworkImage: true
	});
	w.addEventListener("loaded", function() {
		w.show("slide-in-bottom", 300);
		// 加载网络图片
		w.setBlockNetworkImage(false);
	}, false);
}

function tmpCallBack(a, b) {

	switch(b[1]) {
		case "createdAt":
			var timestamp3 = a.createdAt;
			var newDate = new Date();
			newDate.setTime(timestamp3 * 1000);
			var newXDate = new XDate(newDate);
			var showTime = newXDate.toString('HH:mm');
			if(newXDate.toString('yyyy年MM月dd日') == new XDate().toString('yyyy年MM月dd日')) {
				//showTime = new XDate(newDate).toString('HH:mm');
			} else {

			}

			return showTime;
			break;
		case "contentHtml":
			var re = "";

			if(a.contentText.indexOf('黄金') > -1) {
				re = '<font style="color:red;">' + a.contentText + '</font>';
			} else {
				re = a.contentText;
			}

			return re;

			break;

		default:

			break;
	}
	return '';
}

function finCallBack(a, b) {

	switch(b[1]) {
		case "GuoQi":
			var guoQi = document.createElement('div');
			guoQi.innerHTML = a.GuoQi;
			guoQi.firstChild.style.width = '32px';
			guoQi.firstChild.style.height = '32px';

			return guoQi.innerHTML;
			break;
		case "Title":

			return $(a.Title).text();
			break;

		case "JinZhi":

			return $(a.JinZhi).text();
			break;

		default:

			break;
	}
	return '';
}

function openMarket(productcode, productname) {

	openWindowWithTitle("marketView.html?code=" + productcode, productname + '')
}